from rest_framework import serializers
from .models import TestCase, TEST_METHODS, CASE_STATUSES, PRIORITIES

class TestCaseSerializer(serializers.ModelSerializer):
    """测试用例序列化器"""
    # 添加版本信息
    version_name = serializers.ReadOnlyField(source='version.version_number')
    project_name = serializers.ReadOnlyField(source='version.project.name')
    # 添加显示值字段
    test_method_display = serializers.CharField(source='get_test_method_display')
    status_display = serializers.CharField(source='get_status_display')
    priority_display = serializers.CharField(source='get_priority_display')
    
    class Meta:
        model = TestCase
        fields = ['id', 'case_id', 'title', 'description', 'precondition', 'steps', 'expected_result',
                  'actual_result', 'priority', 'priority_display', 'status', 'status_display',
                  'test_method', 'test_method_display', 'created_at', 'updated_at', 'created_by',
                  'reviewed_by', 'reviewed_at', 'is_automated', 'automation_script', 'module',
                  'requirement_id', 'version', 'version_name', 'project_name']
        read_only_fields = ['id', 'created_at', 'updated_at', 'reviewed_at']
    
    def validate(self, data):
        """验证数据"""
        # 确保已评审的用例不能随便修改
        if self.instance and self.instance.status == 'reviewed':
            # 只允许修改特定字段
            allowed_fields = ['status', 'actual_result', 'reviewed_by', 'reviewed_at']
            for field, value in data.items():
                if field not in allowed_fields:
                    raise serializers.ValidationError(f'已评审的用例不允许修改{field}字段')
        return data

class TestCaseCreateSerializer(serializers.ModelSerializer):
    """测试用例创建序列化器"""
    class Meta:
        model = TestCase
        fields = ['case_id', 'title', 'description', 'precondition', 'steps', 'expected_result',
                  'priority', 'status', 'test_method', 'created_by', 'is_automated',
                  'automation_script', 'module', 'requirement_id', 'version']
    
    def validate_case_id(self, value):
        """验证用例ID唯一性"""
        if TestCase.objects.filter(case_id=value).exists():
            raise serializers.ValidationError('用例ID已存在')
        return value

class TestCaseUpdateSerializer(serializers.ModelSerializer):
    """测试用例更新序列化器"""
    class Meta:
        model = TestCase
        fields = ['title', 'description', 'precondition', 'steps', 'expected_result',
                  'actual_result', 'priority', 'status', 'test_method', 'reviewed_by',
                  'is_automated', 'automation_script', 'module', 'requirement_id']
        read_only_fields = ['case_id', 'version']
    
    def validate(self, data):
        """验证数据"""
        # 确保已评审的用例不能随便修改
        if self.instance and self.instance.status == 'reviewed':
            # 只允许修改特定字段
            allowed_fields = ['status', 'actual_result', 'reviewed_by', 'reviewed_at']
            for field, value in data.items():
                if field not in allowed_fields:
                    raise serializers.ValidationError(f'已评审的用例不允许修改{field}字段')
        return data

class TestCaseListSerializer(serializers.ModelSerializer):
    """测试用例列表序列化器"""
    # 添加显示值字段
    test_method_display = serializers.CharField(source='get_test_method_display')
    status_display = serializers.CharField(source='get_status_display')
    priority_display = serializers.CharField(source='get_priority_display')
    # 添加版本和项目信息
    version_name = serializers.ReadOnlyField(source='version.version_number')
    project_name = serializers.ReadOnlyField(source='version.project.name')
    
    class Meta:
        model = TestCase
        fields = ['id', 'case_id', 'title', 'priority', 'priority_display', 'status',
                  'status_display', 'test_method', 'test_method_display', 'module',
                  'created_at', 'updated_at', 'version_name', 'project_name']